package com.dengyafei.service.impl;

import com.dengyafei.bean.EnterpriseEntity;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.constant.RedisKeyConstant;
import com.dengyafei.domin.dto.EnterpriseUpdateDTO;
import com.dengyafei.domin.dto.EnterpriseUpdatePasswordDTO;
import com.dengyafei.domin.dto.ResetPasswordDTO;
import com.dengyafei.domin.vo.*;
import com.dengyafei.exception.RedisListUpdateException;
import com.dengyafei.helper.BeanHelper;
import com.dengyafei.mapper.EnterpriseEntityMapper;
import com.dengyafei.result.RedisLoginEntity;
import com.dengyafei.service.EnterpriseService;
import com.dengyafei.utils.JwtUtil;
import com.dengyafei.utils.LoginSqlUtil;
import com.dengyafei.utils.RedisListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 邓亚非
 */
@Slf4j
@Service
public class EnterpriseServiceImpl implements EnterpriseService {

    @Autowired
    EnterpriseEntityMapper enterpriseEntityMapper;
    @Autowired
    RedisTemplate<Object, Object> redisTemplate;
    @Autowired
    HttpServletRequest request;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 企业注册接口
     *
     * @param enterpriseRegisterVO
     * @return
     */
    @Override
    public Integer enterpriseRegister(EnterpriseRegisterVO enterpriseRegisterVO) {
//        查询是否已经注册
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(enterpriseRegisterVO.getHname());
        if (!ObjectUtils.isEmpty(enterpriseEntity)) {
            return RecommendConstant.ALREADY_REGISTER;
        }
//        注册成功之后写入缓存
        Integer integer = enterpriseEntityMapper.enterpriseRegister(enterpriseRegisterVO);
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            return RecommendConstant.REGISTER_FAILURE;
        }
//        写入缓存
        RedisLoginEntity redisLoginEntity = new RedisLoginEntity();
        redisLoginEntity.setUsername(enterpriseRegisterVO.getHname());
        redisLoginEntity.setPassword(enterpriseRegisterVO.getHpassword());
        Long aLong = redisTemplate.opsForList().leftPush(RedisKeyConstant.EnterpriseRedis.ENTERPRISE_LOGIN, redisLoginEntity);
        System.out.println(aLong);
        return integer;
    }

    /**
     * 企业登录接口
     *
     * @param enterpriseLoginVO
     * @return
     */
    @Override
    public Integer enterpriseLogin(EnterpriseLoginVO enterpriseLoginVO) {
//        查缓存
        RedisLoginEntity redisLoginEntity = BeanHelper.copyProperties(enterpriseLoginVO, RedisLoginEntity.class);
        Integer login = RedisListUtil.getLogin(redisTemplate, RedisKeyConstant.EnterpriseRedis.ENTERPRISE_LOGIN, redisLoginEntity);
        EnterpriseEntity enterpriseEntity = null;
        if (login.equals(RecommendConstant.REDIS_NULL)) {
            enterpriseEntity = enterpriseEntityMapper.queryAllByHname(enterpriseLoginVO.getUsername());
        } else {
            return login;
        }
//        查询mysql
        Integer integer = LoginSqlUtil.queryEnterpriseLoginBySql(enterpriseEntity, enterpriseLoginVO);
        return integer;
    }

    /**
     * 企业用户重置密码
     *
     * @param resetPasswordVO
     * @return
     */
    @Override
    public Integer enterpriseResetPassword(ResetPasswordVO resetPasswordVO) {
        ResetPasswordDTO resetPasswordDTO = BeanHelper.copyProperties(resetPasswordVO, ResetPasswordDTO.class);
        Integer integer = enterpriseEntityMapper.enterpriseFindPassword(resetPasswordDTO);
        if (!integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
            return RecommendConstant.RESET_FAILURE;
        }
//        重置成功修改缓存
        RedisLoginEntity redisLoginEntity = BeanHelper.copyProperties(resetPasswordDTO, RedisLoginEntity.class);
        boolean b = RedisListUtil.updateRedisList(redisTemplate, RedisKeyConstant.EnterpriseRedis.ENTERPRISE_LOGIN, redisLoginEntity);
        if (!b) {
            throw new RedisListUpdateException();
        }
        return integer;
    }

    /**
     * 企业用户查询个人信息
     *
     * @param
     * @return
     */
    @Override
    public EnterpriseEntity enterpriseQueryByName() {
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
        return enterpriseEntity;
    }

    /**
     * 企业用户修改个人信息
     *
     * @param enterpriseUpdateVO
     * @return
     */
    @Override
    public Integer enterpriseUpdateInfo(EnterpriseUpdateVO enterpriseUpdateVO) {
        EnterpriseUpdateDTO enterpriseUpdateDTO = BeanHelper.copyProperties(enterpriseUpdateVO, EnterpriseUpdateDTO.class);
        EnterpriseEntity enterpriseEntity = BeanHelper.copyProperties(enterpriseUpdateDTO, EnterpriseEntity.class);
        return enterpriseEntityMapper.updateByPrimaryKey(enterpriseEntity);
    }

    /**
     * 认证身份接口
     *
     * @param enterpriseUpdatePasswordVO
     * @return
     */
    @Override
    public Boolean checkEnterprise(EnterpriseUpdatePasswordVO enterpriseUpdatePasswordVO) {
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(enterpriseUpdatePasswordVO.getName());
        if (enterpriseEntity.getHpassword().equals(enterpriseUpdatePasswordVO.getPassword())) {
            return true;
        }
        return false;
    }

    /**
     * 修改密码
     *
     * @param enterpriseUpdatePasswordVO
     * @return
     */
    @Override
    public Integer updatePassword(EnterpriseUpdatePasswordVO enterpriseUpdatePasswordVO) {
        EnterpriseUpdatePasswordDTO enterpriseUpdatePasswordDTO = BeanHelper.copyProperties(enterpriseUpdatePasswordVO, EnterpriseUpdatePasswordDTO.class);
        Integer integer = enterpriseEntityMapper.updatePassword(enterpriseUpdatePasswordDTO);
        if (integer.equals(RecommendConstant.OPERATION_SUCCESS)) {
//            修改redis
            List<Object> range = redisTemplate.opsForList().range(RedisKeyConstant.EnterpriseRedis.ENTERPRISE_LOGIN, 0, -1);
            List<RedisLoginEntity> redisLoginEntities = BeanHelper.copyWithCollection(range, RedisLoginEntity.class);
            long index = 0;
            RedisLoginEntity redisLoginEntity = null;
            for (int i = 0; i < redisLoginEntities.size(); i++) {
                if (redisLoginEntities.get(i).getUsername().equals(enterpriseUpdatePasswordVO.getName())) {
                    index = i;
                    redisLoginEntity = redisLoginEntities.get(i);
                }
            }
            redisLoginEntity.setPassword(enterpriseUpdatePasswordVO.getNewPassword());
            redisTemplate.opsForList().set(RedisKeyConstant.EnterpriseRedis.ENTERPRISE_LOGIN, index, redisLoginEntity);
        }
        return integer;
    }

    /**
     * 保存头像
     * @param enterpriseHeadImageVO
     * @return
     */
    @Override
    public Boolean saveImg(EnterpriseHeadImageVO enterpriseHeadImageVO) {
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
        String key=RedisKeyConstant.HeadImageRedis.ENTERPRISE_HEAD_IMAGE_PREFIX+enterpriseEntity.getHid();
        try {
            stringRedisTemplate.opsForValue().set(key,enterpriseHeadImageVO.getImgUrl());
            return true;
        } catch (Exception e) {
            log.info("更新成功");
        }
        return false;
    }

    /**
     * 加载企业用户头像
     * @return
     */
    @Override
    public String queryImg() {
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseEntity enterpriseEntity = enterpriseEntityMapper.queryAllByHname(userInfo);
        String key=RedisKeyConstant.HeadImageRedis.ENTERPRISE_HEAD_IMAGE_PREFIX+enterpriseEntity.getHid();
        return stringRedisTemplate.opsForValue().get(key);
    }

    /**
     * 查询所有企业用户
     * @return
     */
    @Override
    public List<EnterpriseEntity> queryEnterprises() {
        return enterpriseEntityMapper.queryEnterprises();
    }
}